define(['jquery', 'bootstrap', 'backend', 'table', 'form','previewfile'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'help_task/index' + location.search,
                    add_url: 'help_task/add',
                    edit_url: 'help_task/edit',
                    // del_url: 'help_task/del',
                    multi_url: 'help_task/multi',
                    import_url: 'help_task/import',
                    table: 'help_task',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                // commonSearch: false,
                search:false,
                showToggle: false,
                showColumns: false,
                visible: false,
                // showExport: false,
                fixedColumns: true,
                pageSize: 10,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'user.nickname', title: __('User.nickname'), operate: false},
                        {field: 'user.mobile', title: '發佈用戶手機號', operate: false},
                        {field: 'score', title: '積分數量', operate: false},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {'1': __('Status 1'), '2': __('Status 2'),'3':__('Status 3'),'4':__('Status 4'),'5':__('Status 5'),'6':__('Status 6')}},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter:function(value,row,index){
                                    this.buttons = [{
                                            name: 'confirm',
                                            text: '確認',
                                            classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                            url: 'help_task/confirm',
                                            confirm: '是否確認完成？',
                                            visible: function (row) {
                                                if ( Config.help_confirm && row.status == 5) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },{
                                            name: 'help_score',
                                            text: '評分',
                                            classname: 'btn btn-xs btn-warning btn-magic btn-dialog',
                                            url: 'help_task/score',
                                            // confirm: '确认禁用嗎？',
                                            visible: function (row) {
                                                if ( Config.help_score && row.status == 6 && row.grade_status == 1) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            callback: function (data) {            //回调方法，用来响应 Fast.api.close()方法 **注意不能有success 是btn-ajax的回调，btn-dialog 用的callback回调，两者不能同存！！！！
                                                $(".btn-refresh").trigger("click");//刷新当前页面的数据
                                                console.error(data);//控制输出回调数据
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true
                                        },
                                        {
                                            name: 'audit',
                                            text: '審核',
                                            classname: 'btn btn-xs btn-danger btn-magic btn-dialog',
                                            url: 'help_task/audit',
                                            // confirm: '确认禁用嗎？',
                                            visible: function (row) {
                                                if ( Config.help_audit && row.status == 1) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },
                                        {
                                            name: 'volunteer',
                                            text: '分配義工',
                                            classname: 'btn btn-xs btn-warning btn-magic btn-dialog',
                                            url: 'volunteer/indexsss',
                                            // confirm: '确认禁用嗎？',
                                            visible: function (row) {
                                                if ( Config.volunteer && row.status == 3) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            callback: function (data) {            //回调方法，用来响应 Fast.api.close()方法 **注意不能有success 是btn-ajax的回调，btn-dialog 用的callback回调，两者不能同存！！！！
                                                $(".btn-refresh").trigger("click");//刷新当前页面的数据
                                                console.error(data);//控制输出回调数据
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true
                                        },{
                                            name: 'edit',
                                            text: '詳情',
                                            classname: 'btn btn-xs btn-success btn-magic btn-editone',
                                            url: 'help_task/edit',
                                            // confirm: '确认禁用嗎？',
                                            visible: function (row) {
                                                if ( Config.help_audit_edit ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        }
                                    ];
                                return Table.api.formatter.operate.call(this,value,row,index);
                            }}
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        audit: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        score: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});